package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PerformanceInterview;
import com.amsp.oa.oademo.mapper.PerformanceInterviewMapper;
import com.amsp.oa.oademo.service.PerformanceInterviewService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class PerformanceInterviewServiceImpl extends ServiceImpl<PerformanceInterviewMapper, PerformanceInterview>
        implements PerformanceInterviewService {

    @Override
    public IPage<PerformanceInterview> getPage(Integer current, Integer size, String appraiser, String employeeId,
            String department, String interviewType, String startTime, String endTime) {
        LambdaQueryWrapper<PerformanceInterview> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        wrapper.like(StringUtils.isNotBlank(appraiser), PerformanceInterview::getAppraiser, appraiser)
                .like(StringUtils.isNotBlank(employeeId), PerformanceInterview::getEmployeeId, employeeId)
                .like(StringUtils.isNotBlank(department), PerformanceInterview::getDepartment, department);

        // 处理面谈类型
        if (StringUtils.isNotBlank(interviewType)) {
            switch (interviewType) {
                case "monthlyPlan":
                    wrapper.eq(PerformanceInterview::getMonthlyPlan, true);
                    break;
                case "lastMonthReview":
                    wrapper.eq(PerformanceInterview::getLastMonthReview, true);
                    break;
                case "bothType":
                    wrapper.eq(PerformanceInterview::getBothType, true);
                    break;
            }
        }

        // 处理时间范围
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            wrapper.between(PerformanceInterview::getInterviewTime, startTime, endTime + " 23:59:59");
        }

        // 按面谈时间倒序排序
        wrapper.orderByDesc(PerformanceInterview::getInterviewTime);

        return page(new Page<>(current, size), wrapper);
    }
}